package net.loyintean.pearlshell.validation.rule;

import java.util.regex.Matcher;
import java.util.regex.Pattern;

import net.loyintean.pearlshell.validation.IValidRule;
import net.loyintean.pearlshell.validation.IValidRuleConfig;

class StringIdentifyCardValidRuleImpl extends AbstractStringValidRuleImpl implements IValidRule {

	StringIdentifyCardValidRuleImpl() {
		super();
	}

	private static final Pattern IDENTIFY_CARD = Pattern.compile("(\\d{15})|(\\d{17}[0-9xX])");

	@Override
	public String errorMsg(IValidRuleConfig config) {

		return "不符合身份证号的格式";
	}

	@Override
	protected boolean validString(String toValid, IValidRuleConfig config) {

		if (toValid.length() == 18 || toValid.length() == 15) {
			Matcher indetify_Card = IDENTIFY_CARD.matcher(toValid);
			return indetify_Card.find();
		}
		return false;
	}
}
